package com.swak.frame.dto.response;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.swak.frame.dto.base.DTO;
import com.swak.frame.dto.base.RestApiCode;
import com.swak.frame.dto.base.SysRestCode;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel
public class Response implements DTO {
	private static final long serialVersionUID = 7885784556539697892L;

	@ApiModelProperty(value = "响应CODE,200=成功")
	protected String retCode = SysRestCode.SUCC.getRetCode();

	@ApiModelProperty(value = "响应消息")
	protected String retMessage;

	/** 成功返回的对象 **/
	public static Response SUCC = new Response(SysRestCode.SUCC);

	/** 失败返回的对象 **/
	public static Response FAIL = new Response(SysRestCode.SYS_ERR);

	public Response(String retCode, String retMessage) {
		this.retCode = retCode;
		this.retMessage = retMessage;
	}

	public Response(RestApiCode restCode) {
		this(restCode.getRetCode(), restCode.getRetMessage());
	}

	public Response() {
		this(SysRestCode.SUCC);
	}

	public boolean eq(String retCode) {
		return Objects.equals(this.getRetCode(), retCode);
	}

	public boolean retSystemErr() {
		return this.eq(SysRestCode.SYS_ERR.getRetCode());
	}

	public boolean retRpcTimeOut() {
		return this.eq(SysRestCode.SYS_RPC_TIMEOUT.getRetCode());
	}

	public String getRetCode() {
		return retCode;
	}

	public void setRetCode(String retCode) {
		this.retCode = retCode;
	}

	public String getRetMessage() {
		return retMessage;
	}

	public void setRetMessage(String retMessage) {
		this.retMessage = retMessage;
	}

	public boolean isSuccess() {
		return Objects.equals(SysRestCode.SUCC.getRetCode(), StringUtils.trim(getRetCode()));
	}

	public static Response.Builder builder() {
		return new Response.Builder();
	}

	public Response of(RestApiCode restCode) {
		this.retCode = restCode.getRetCode();
		this.retMessage = restCode.getRetMessage();
		return this;
	}

	public Response of(String retCode, String retMessage) {
		this.retCode = retCode;
		this.retMessage = retMessage;
		return this;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

	public static class Builder {
		protected String retCode;
		protected String retMessage;

		public Builder() {
			this.retCode = SysRestCode.SUCC.getRetCode();
			this.retMessage = SysRestCode.SUCC.getRetMessage();
		}

		public Builder retCode(String retCode) {
			this.retCode = retCode;
			return this;
		}

		public Builder retMessage(String retMessage) {
			this.retMessage = retMessage;
			return this;
		}

		public Builder of(RestApiCode restCode) {
			this.retCode = restCode.getRetCode();
			this.retMessage = restCode.getRetMessage();
			return this;
		}

		public Builder of(String retCode, String retMessage) {
			this.retCode = retCode;
			this.retMessage = retMessage;
			return this;
		}

		public Response build() {
			return new Response(this);
		}
	}

	protected Response(Builder builder) {
		this.retCode = builder.retCode;
		this.retMessage = builder.retMessage;
	}

	public static void main(String[] args) {
		System.out.println(JSON.toJSONString(Response.FAIL));
	}
}
